import Objects from "../../utils/objects";

function VisualAnalysis2(options) {
    this.viewer = options.viewer;
    VisualAnalysis2.CURRENT = this;
    this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas);
    this.viewPoint = 0;

    this.direction = options.direction;
    this.horizontalViewAngle = options.horizontalViewAngle;
    this.visualRange = options.visualRange;
    this.viewPosition = options.viewPosition;

    this.updata();
}

VisualAnalysis2.CURRENT = undefined;

VisualAnalysis2.prototype.updata = function() {
    let that = this;
    that.clear();

    let cartesian = that.viewPosition;

    let ellipsoid = that.viewer.scene.globe.ellipsoid;
    let wgs84 = ellipsoid.cartesianToCartographic(cartesian);

    that.pointll = [Cesium.Math.toDegrees(wgs84.longitude), Cesium.Math.toDegrees(wgs84.latitude), wgs84.height + 10];
    // console.log(that.pointll);
    that.click(that.pointll);
    that.pointll = 0;
};

VisualAnalysis2.prototype._getCartesian3FromPosition = function(position, ...noPickEntityArray) {
    let that = this;

    let picks = this.viewer.scene.drillPick(position);
    this.viewer.render();

    let getOnModel = false;
    let cartesian3 = null;

    if (this.viewer.scene.pickPositionSupported) {
        for (let i = 0; i < picks.length; i++) {
            let pickedObject = picks[i];
            for (let j = 0; j < noPickEntityArray.length; j++) {
                let noPickEntity = noPickEntityArray[j];
                if (that._hasPickedModel(pickedObject, noPickEntity)) {
                    getOnModel = true;
                }
            }
        }
    }

    if (getOnModel) {
        cartesian3 = this.viewer.scene.pickPosition(position);
    } else {
        let ray = this.viewer.camera.getPickRay(position);
        if (Objects.isNull(ray)) {
            return null;
        }
        cartesian3 = this.viewer.scene.globe.pick(ray, this.viewer.scene);
    }
    return cartesian3;
};

VisualAnalysis2.prototype.click = function(ll) {
    let that = this;
    that.viewer.scene.globe.depthTestAgainstTerrain = true;
    // 世界坐标转换为投影坐标
    //console.log(that.viewPoint);
    that.viewPoint = Cesium.Cartesian3.fromDegrees(ll[0], ll[1], ll[2]);
    //console.log(that.viewPoint);

    let webMercatorProjection = new Cesium.WebMercatorProjection(that.viewer.scene.globe.ellipsoid);
    let viewPointWebMercator = webMercatorProjection.project(Cesium.Cartographic.fromCartesian(that.viewPoint));

    //console.log(viewPointWebMercator);

    // 目标点集合
    that.destPoints = [];

    // 观察点和目标点的距离
    let radius = that.visualRange; // 视距1000米
    let range = that.horizontalViewAngle + that.direction; //可视范围
    //console.log(range);

    // 计算45°和135°之间的目标点
    for (let i = that.direction; i <= range; i += 5) {
        // 度数转弧度
        let radians = Cesium.Math.toRadians(i);
        // 计算目标点
        let toPoint = new Cesium.Cartesian3(viewPointWebMercator.x + radius * Math.cos(radians), viewPointWebMercator.y + radius * Math.sin(radians), ll[2]);
        // 投影坐标转世界坐标
        // console.log(toPoint);
        // console.log(that.viewPoint);

        // let distance = Cesium.Cartesian3.distance(viewPointWebMercator, toPoint);
        // console.log(distance);

        toPoint = webMercatorProjection.unproject(toPoint);

        that.destPoints.push(Cesium.Cartographic.toCartesian(toPoint.clone()));
    }
    that.pickFromRay();
};

VisualAnalysis2.prototype.drawLine = function(leftPoint, secPoint, color) {
    this.viewer.entities.add({
        polyline: {
            positions: [leftPoint, secPoint],
            arcType: Cesium.ArcType.NONE,
            width: 5,
            material: color,
            depthFailMaterial: color
        }
    });
};

VisualAnalysis2.prototype.pickFromRay = function() {
    for (let i = 0; i < this.destPoints.length; i++) {
        // this.viewpoint代表中心点
        // this.destPoints代表每个角度终点
        // 计算射线的方向，目标点left 视域点right
        let direction = Cesium.Cartesian3.normalize(Cesium.Cartesian3.subtract(this.destPoints[i], this.viewPoint, new Cesium.Cartesian3()), new Cesium.Cartesian3());
        // 建立射线
        // console.log(direction);
        let ray = new Cesium.Ray(this.viewPoint, direction);
        // console.log(ray);
        //let result = this.viewer.scene.pickFromRay(ray); // 计算交互点，返回第一个

        let result = this.viewer.scene.globe.pick(ray, this.viewer.scene);
        //console.log(result);

        // console.log(this.viewPoint);
        // console.log(result);

        if (Cesium.defined(result)) {
            let distance = Cesium.Cartesian3.distance(result, this.viewPoint);
            console.log(distance);

            if (distance < this.visualRange - 60) {
                this.showIntersection(result, this.destPoints[i], this.viewPoint);
            } else {
                this.drawLine(this.destPoints[i], this.viewPoint, Cesium.Color.GREEN);
            }
        } else {
            this.showIntersection(result, this.destPoints[i], this.viewPoint);
        }
    }
};

// 处理交互点
VisualAnalysis2.prototype.showIntersection = function(result, destPoint, viewPoint) {
    // 如果是场景模型的交互点，排除交互点是地球表面
    if (Cesium.defined(result)) {
        this.drawLine(result, viewPoint, Cesium.Color.GREEN); // 可视区域
        this.drawLine(result, destPoint, Cesium.Color.RED); // 不可视区域
    } else {
        this.drawLine(viewPoint, destPoint, Cesium.Color.GREEN);
    }
};

VisualAnalysis2.prototype.clear = function() {
    console.log("******************************************************");
    let that = this;
    that.viewer.entities.removeAll();
};

export default VisualAnalysis2;
